﻿using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Guids;
using Volo.Abp.Identity;
using Volo.Abp.MultiTenancy;
using Volo.Abp.Uow;

namespace GMall.AdministrationService.DataSeed
{
    public class IdentityDataSeeder : Volo.Abp.Identity.IdentityDataSeeder
    {
        public IdentityDataSeeder(IGuidGenerator guidGenerator, IIdentityRoleRepository roleRepository, IIdentityUserRepository userRepository, ILookupNormalizer lookupNormalizer, IdentityUserManager userManager, IdentityRoleManager roleManager, ICurrentTenant currentTenant, IOptions<IdentityOptions> identityOptions) : base(guidGenerator, roleRepository, userRepository, lookupNormalizer, userManager, roleManager, currentTenant, identityOptions)
        {
        }
        [UnitOfWork]
        public override async Task<IdentityDataSeedResult> SeedAsync(
        string adminEmail,
        string adminPassword,
        Guid? tenantId = null)
        {
            adminEmail = "admin@5411.xyz";
            adminPassword = "1q2w3E*";
            Check.NotNullOrWhiteSpace(adminEmail, nameof(adminEmail));
            Check.NotNullOrWhiteSpace(adminPassword, nameof(adminPassword));
            using (CurrentTenant.Change(tenantId))
            {
                await IdentityOptions.SetAsync();

                var result = new IdentityDataSeedResult();
                //"admin" user
                const string adminUserName = "admin";
                var adminUser = await UserRepository.FindByNormalizedUserNameAsync(
                    LookupNormalizer.NormalizeName(adminUserName)
                );

                if (adminUser != null)
                {
                    return result;
                }

                adminUser = new IdentityUser(
                    GuidGenerator.Create(),
                    adminUserName,
                    adminEmail,
                    tenantId
                )
                {
                    Name = adminUserName
                };

                (await UserManager.CreateAsync(adminUser, adminPassword, validatePassword: false)).CheckErrors();
                result.CreatedAdminUser = true;

                //"admin" role
                const string adminRoleName = "admin";
                var adminRole =
                    await RoleRepository.FindByNormalizedNameAsync(LookupNormalizer.NormalizeName(adminRoleName));
                if (adminRole == null)
                {
                    adminRole = new IdentityRole(
                        GuidGenerator.Create(),
                        adminRoleName,
                        tenantId
                    )
                    {
                        IsStatic = true,
                        IsPublic = true
                    };

                    (await RoleManager.CreateAsync(adminRole)).CheckErrors();
                    result.CreatedAdminRole = true;
                }

                (await UserManager.AddToRoleAsync(adminUser, adminRoleName)).CheckErrors();

                return result;
            }
        }
    }

    //public class IdentityDataSeedContributor : Volo.Abp.Identity.IdentityDataSeedContributor
    //{
    //    public IdentityDataSeedContributor(IIdentityDataSeeder identityDataSeeder) : base(identityDataSeeder)
    //    {
    //    }
    //    public override Task SeedAsync(DataSeedContext context)
    //    {
    //        var adminEmail = "admin@5411.xyz";
    //        var adminPassword = "1q2w3E*";
    //        return IdentityDataSeeder.SeedAsync(adminEmail, adminPassword, context?.TenantId);
    //    }
    //}
}
